<template>
	<view class="container">
		<!-- <u-navbar bgColor="#0065FF" titleStyle="color:#fff;" leftIconColor="#fff" title="修改密码" autoBack
			placeholder></u-navbar> -->
		<view class="content">
			<u-input type="password" placeholder="请输入原密码" border="surround" v-model="oldPassword"></u-input>
			<u-input type="password" placeholder="请输入新密码" border="surround" v-model="password"></u-input>
			<u-input type="password" placeholder="请确认新密码" border="surround" v-model="passwordAgain"></u-input>
			<view class="btn">
				<u-button @click="editPwd" :loading="loading" color="#0065FF" text="确定修改"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				oldPassword:'',
				password: '',
				passwordAgain: ''
			};
		},
		methods: {
			async editPwd() {
				if (!this.oldPassword) {
					uni.$u.toast('请输入原密码');
					return;
				}
				if (!this.password) {
					uni.$u.toast('请输入新密码');
					return;
				}
				if (!this.passwordAgain) {
					uni.$u.toast('请再次输入新密码');
					return;
				}
				if (this.password != this.passwordAgain) {
					uni.$u.toast('两次密码输入不一致');
					return;
				}
				this.loading = true;
				this.$api.deitPwd({
					newPassword: this.passwordAgain,
					oldPassword: this.oldPassword
				}).then((response) => {
					this.loading = false;
					if (response.code == 200) {
						uni.$u.toast('修改成功，请重新登录');
						this.$store.commit('user/setUserInfo', {
							id: null,
							isLogin: false,
							access_token: null,
							avatar: null,
							phonenumber: null,
							nickName: null,
							userName: null,
							people: {},
							dept: {},
						})
						setTimeout(function() {
							
							uni.reLaunch({
								url: '/pages/login'
							});
						}, 1000);
						// uni.clearStorageSync();
					} else {
						uni.$u.toast(response.msg);
					}
				})
				return
				const {
					code,
					data
				} = await this.$api.userEdit({
					password: this.password
				});
				this.loading = false;
				if (code == 200) {
					uni.$u.toast('修改成功，请重新登录');
					setTimeout(function() {
						uni.reLaunch({
							url: '/pages/firing'
						});
					}, 1000);
					uni.clearStorageSync();

				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.content {
		padding: 60rpx 20rpx;

		/deep/ .u-input {
			margin-bottom: 20rpx;
		}

		.btn {
			margin-top: 60rpx;

			/deep/ .u-button {
				border-radius: 20rpx;
			}
		}
	}
</style>